import 'dart:async';

import 'package:account/utils/cache.dart';
import 'package:account/utils/my_data.dart';
import 'package:flutter/material.dart';

import '../utils/my_data.dart';
import 'login.dart';
import 'main_page.dart';

void main() {
  runApp(GuidApp());
}

class GuidApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return GuidAppState();
  }
}

class GuidAppState extends State {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: GuidHomePage());
  }
}

class GuidHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    init(context);

    Size size = MediaQuery.of(context).size;
    var width = size.width;
    var height = size.height;

    return Scaffold(
        body: Container(
      width: width,
      height: height,
      child: Image.asset("images/back.jpg", fit: BoxFit.fill),
    ));
  }

  Future<void> init(BuildContext context) async {
    //初始化缓存内容
    await Cache.init();
    await MyData.init();

    //读取所有信息
    // QUtils.listAccount = (await MyData.getAllAccount())!;
    await MyData.getAllGroupAndAccount();
    //获取账号密码内容
    Cache.getUser().then((value) {
      Cache.user = value;
      Timer(Duration(seconds: 3), () {
        if (Cache.user!.pwd == null) {
          Navigator.pushAndRemoveUntil(
            context,
            new MaterialPageRoute(builder: (context) => new MainPageApp()),
            (route) => false,
          );
        } else {
          Navigator.pushAndRemoveUntil(
            context,
            new MaterialPageRoute(builder: (context) => new LoginApp()),
            (route) => false,
          );
        }
      });
    });
  }
}
